package com.lsa.letcode.the74.search2dMatrix;

import static org.junit.Assert.*;

import org.junit.Test;

public class Search2dMatrixTest {
	@Test
	public void test() throws Exception {
		test(new int[][] { { 1 } }, 1,
				new int[] { 0, 0 });
		test(new int[][] { { 1 } }, 2,
				new int[] { -1, -1 });
		
		test(new int[][] { { 1, 2, 3 }, { 5, 7, 9 }, { 11, 14, 17 } }, 1,
				new int[] { 0, 0 });
		test(new int[][] { { 1, 2, 3 }, { 5, 7, 9 }, { 11, 14, 17 } }, 2,
				new int[] { 0, 1 });
		test(new int[][] { { 1, 2, 3 }, { 5, 7, 9 }, { 11, 14, 17 } }, 3,
				new int[] { 0, 2 });
		test(new int[][] { { 1, 2, 3 }, { 5, 7, 9 }, { 11, 14, 17 } }, 4,
				new int[] { -1, -1 });
		test(new int[][] { { 1, 2, 3 }, { 5, 7, 9 }, { 11, 14, 17 } }, 5,
				new int[] { 1, 0 });
		test(new int[][] { { 1, 2, 3 }, { 5, 7, 9 }, { 11, 14, 17 } }, 7,
				new int[] { 1, 1 });
		test(new int[][] { { 1, 2, 3 }, { 5, 7, 9 }, { 11, 14, 17 } }, 8,
				new int[] { -1, -1 });
		test(new int[][] { { 1, 2, 3 }, { 5, 7, 9 }, { 11, 14, 17 } }, 9,
				new int[] { 1, 2 });
		test(new int[][] { { 1, 2, 3 }, { 5, 7, 9 }, { 11, 14, 17 } }, 11,
				new int[] { 2, 0 });
		test(new int[][] { { 1, 2, 3 }, { 5, 7, 9 }, { 11, 14, 17 } }, 14,
				new int[] { 2, 1 });
		test(new int[][] { { 1, 2, 3 }, { 5, 7, 9 }, { 11, 14, 17 } }, 17,
				new int[] { 2, 2 });
		
		test(new int[][] {
				  {1,   3,  5,  7},
				  {10, 11, 16, 20},
				  {23, 30, 34, 50}
				}, 1,
				new int[] { 0, 0 });
		
		test(new int[][] {
				  {1,   3,  5,  7},
				  {10, 11, 16, 20},
				  {23, 30, 34, 50}
				}, 3,
				new int[] { 0, 1});
		
		test(new int[][] {
				  {1,   3,  5,  7},
				  {10, 11, 16, 20},
				  {23, 30, 34, 50}
				}, 5,
				new int[] { 0, 2});
		
		test(new int[][] {
				  {1,   3,  5,  7},
				  {10, 11, 16, 20},
				  {23, 30, 34, 50}
				}, 7,
				new int[] { 0, 3});
		
		test(new int[][] {
				  {1,   3,  5,  7},
				  {10, 11, 16, 20},
				  {23, 30, 34, 50}
				}, 16,
				new int[] { 1, 2});
		
		test(new int[][] {
				  {1,   3,  5,  7},
				  {10, 11, 16, 20},
				  {23, 30, 34, 50}
				}, 30,
				new int[] { 2, 1});
		test(new int[][] {
				  {1,   3,  5,  7},
				  {10, 11, 16, 20},
				  {23, 30, 34, 50}
				}, 50,
				new int[] { 2, 3});
		
		test(new int[][] { 
				{ 1, 2, 3 }, 
				{ 5, 7, 9 }, 
				{ 11, 13, 15 },
				{ 17, 19, 21 }, 
				}, 11, new int[] { 2, 0 });
		
		test(new int[][] { 
				{ 1, 2, 3 }, 
				{ 5, 7, 9 }, 
				{ 11, 13, 15 },
				{ 17, 19, 21 }, 
				}, 13, new int[] { 2, 1 });
		
		test(new int[][] { 
				{ 1, 2, 3 }, 
				{ 5, 7, 9 }, 
				{ 11, 13, 15 },
				{ 17, 19, 21 }, 
				}, 21, new int[] { 3, 2 });
	}

	private void test(int[][] m, int t, int[] expectedIdxs) {
		Solution s = new Solution();
		assertArrayEquals(expectedIdxs, s.searchMatrixIdx(m, t));
	}
}
